package com.dong.payment.controller;

import com.dong.payment.service.PaymentService;
import com.dong.springcloud.entities.CommonResult;
import com.dong.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 支付控制层
 * </p>
 *
 * @author: DJ
 * @Date: 15:40 2021/12/25
 */
@RestController
@Slf4j
// @AllArgsConstructor
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/payment/insert")
    public CommonResult insert(@RequestBody Payment payment) {
        int result = paymentService.insert(payment);
        log.info("插入结果为>>>>{},serverPort: {}", result, serverPort);
        if (result > 0) {
            return new CommonResult(200, "数据插入成功");
        } else {
            return new CommonResult(444, "数据插入失败");
        }
    }


    @GetMapping("/payment/get/{id}")
    public CommonResult insert(@PathVariable(value = "id") Long id) {
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询结果为>>>>{}", payment);
        if (Objects.nonNull(payment)) {
            return new CommonResult(200, "查询数据成功,serverPort:" + serverPort, payment);
        } else {
            return new CommonResult(444, "没有对应记录,查询ID:" + id + "serverPort:"  + serverPort, null);
        }
    }

    @GetMapping("/payment/lb")
    public String getServerPortLb() {
        return serverPort;
    }
}
